#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Pablo
#
# Created:     06/05/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os
from pygame.locals import *

class VideoTitle():
  """ Draws experimental video splash screens"""

  def __init__(self, file, surface):
    self.video = pygame.movie.Movie(os.path.join('resources/title/' + file))
    self.first = True
    self.surface = surface

  def draw(self):

    if self.first == True:
      self.video.set_display(self.surface)
      self.video.play()


    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        return 0
      elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
          self.video.stop()
          return 1


    if self.video.get_busy() == False:
      self.video.stop()
      return 1


class ImageTitle():

  """ Draws image based intros """

  def __init__(self, screen):

    self.screen = screen
    self.centerwidth = (self.screen.get_width() / 2) - 250
    self.centerheight = (self.screen.get_height() / 2) - 85
    self.pos = (self.centerwidth, self.centerheight)

    self.count = 0 # counting variable
    self.framecount = 20
    self.framecount2 = 20
    self.movecount = 15
    self.logosound = pygame.mixer.Sound(os.path.join('resources/title/Monxcleyr.ogg'))
    self.logoline = pygame.image.load(os.path.join('resources/title/logoline.bmp'))
    self.logo = pygame.image.load(os.path.join('resources/title/logo.bmp'))
    self.logo1 = pygame.image.load(os.path.join('resources/title/logo1.png'))
    self.logo2 = pygame.image.load(os.path.join('resources/title/logo2.png'))
    self.engine = pygame.image.load(os.path.join('resources/title/hj.png'))
    self.fadesurf = pygame.Surface((500,117))
    self.logosurf = pygame.Surface((500,117))
    self.enginesurf = pygame.Surface((self.screen.get_width(), self.screen.get_height()))
    self.fadesurf.fill((0,0,0))
    self.framelist = [(0,0,500,117), (0,117,500,117), (0,234,500,117), (0,351,500,117), (0,468,500,117), \
    (0,585,500,117), (0,702,500,117), (0,819,500,117), (0,936,500,117), (0,1053,500,117), (0,1170,500,117), \
    (0,1287,500,117)]

    self.firstlogo = True
    self.firstengine = True
    self.logoalphastep = (255 / self.framecount) # the alpha settings
    self.logoslidemove = ((self.screen.get_width() - self.centerwidth) / self.movecount) # set moves for slide

    self.logofadein = True
    self.logoslidein = False
    self.logoanimate = False
    self.logohold = False
    self.logofadeout = False

    self.enginepos = ((self.screen.get_width() / 2- (self.engine.get_width() / 2)), \
        (self.screen.get_height() / 2- (self.engine.get_height() / 2)))
    self.enginealphastep = (255 / self.framecount2) # the alpha settings

    self.enginefadein = True
    self.enginefadeout = False
    self.enginehold = False
    self.enginehold2 = False

    self.status = 0
    self.status2 = 0


  def draw(self, mode=1):

    """ modes:
        0 - Monxcleyr Logo only
        1 - Monxcleyr and Engine Logos
        2 - Engine Logo only"""

    # Draw the logo sequence
    if mode == 0:
      check = self.logo_draw()
      if check == 'done':
        return 1
      elif check == 'exit':
        return 0

    elif mode == 1:
      if self.status == 0:
        check = self.logo_draw()
        if check == 'done':
          self.status = 1
        elif check == 'exit':
          self.status = 3
        elif check == 'skip':
          self.status = 4
      elif self.status == 1:
        check = self.engine_draw()
        if check == 'done':
          self.status = 2
        elif check == 'exit':
          self.status = 3


      if self.status == 2 or self.status == 4:
        return 1
      elif self.status == 3:
        return 0

    elif mode == 2:
      check = self.engine_draw()
      if check == 'done':
        return 1
      elif check == 'exit':
        return 0



  def logo_draw(self):

    if self.firstlogo == True:

      self.count = self.framecount # set self.count for the fadein effect
      self.firstlogo = False



    for event in pygame.event.get():
      if event.type == QUIT:
        running = False
        return 'exit'
      elif event.type == KEYDOWN:
        if event.key == K_ESCAPE:
          running = False
          return 'skip'

    self.screen.fill((0,0,0))

    if self.logofadein == True:
      if self.count != 0:
        self.screen.blit(self.logoline, self.pos)
        self.fadesurf.set_alpha((self.logoalphastep * self.count ))
        self.screen.blit(self.fadesurf, self.pos)
        self.count -= 1
                # play sound
        if self.count == self.framecount - 1:
          pygame.mixer.Sound.play(self.logosound)
      else:
        self.count = 0
        self.logofadein = False
        self.logoslidein = True

    if self.logoslidein == True:
      if self.count != self.logoslidemove:
        self.screen.blit(self.logoline, self.pos)
        self.screen.blit(self.logo2, ((self.screen.get_width() - self.count * self.movecount), self.centerheight))
        self.screen.blit(self.logo1, ((self.count * self.movecount - 500), self.centerheight))
        self.count += 1
      else:
        self.count = 0
        self.logoslidein = False
        self.logoanimate = True

    if self.logoanimate == True:
      if self.count != 12:
        self.logosurf.blit(self.logo, (0,0), self.framelist[self.count])
        self.screen.blit(self.logosurf, self.pos)
        self.count += 1
      else:
        self.count = 0
        self.logoanimate = False
        self.logohold = True


    if self.logohold == True:
      if self.count != 80:
        self.logosurf.blit(self.logo, (0,0), self.framelist[11])
        self.screen.blit(self.logosurf, self.pos)
        self.count += 1
      else:
        self.count = 0
        self.logohold = False
        self.logofadeout = True


    if self.logofadeout == True:
      if self.count != self.framecount:
        self.logosurf.blit(self.logo, (0,0), self.framelist[11])
        self.fadesurf.set_alpha((self.logoalphastep * self.count ))
        self.screen.blit(self.logosurf, self.pos)
        self.screen.blit(self.fadesurf, self.pos)
        self.count += 1
      else:
        self.logofadeout = False
        self.screen.fill((0,0,0))
        return 'done'


  def engine_draw(self):

    if self.firstengine == True:
      self.count = 0
      self.firstengine = False

    for event in pygame.event.get():
      if event.type == QUIT:
        running = False
        return 'exit'
      if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
          running = False
          return 'done'

    self.screen.fill((0,0,0))

    if self.enginefadein == True:
      if self.count != self.framecount2:
        self.enginesurf.blit(self.engine, (0,0))
        self.enginesurf.set_alpha((self.enginealphastep * self.count ))
        self.screen.blit(self.enginesurf, self.enginepos)
        self.count += 1
      else:
        self.count = 0
        self.enginefadein = False
        self.enginehold = True

    if self.enginehold == True:
      if self.count != 80:
        self.screen.blit(self.engine, self.enginepos)
        self.count += 1
      else:
        self.count = self.framecount # set self.count for the fadein effect
        self.enginehold = False
        self.enginefadeout = True

    if self.enginefadeout == True:
      if self.count != 0:
        self.enginesurf.blit(self.engine, (0,0))
        self.enginesurf.set_alpha((self.enginealphastep * self.count ))
        self.screen.blit(self.enginesurf, self.enginepos)
        self.count -= 1
      else:
        self.count = 0
        self.enginefadeout = False
        self.enginehold2 = True
        self.screen.fill((0,0,0))

    if self.enginehold2 == True:
      if self.count != 15:
        self.count += 1
      else:
        self.enginehold2 = False
        return 'done'
